/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午4:16 Created by dwzhang
 */
package com.jade.framework.base.criterion.expression;

import com.jade.framework.base.criterion.Constants;
import com.jade.framework.base.criterion.Criterion;
import com.jade.framework.base.util.SimpleParameters;
import org.apache.commons.lang3.StringUtils;

/**
 * 匹配操作，主要适用于搜索引擎的搜索，在后端使用数据库的情况下，将转换为like操作。
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午4:16
 */
public class MatchExpression extends SimpleParameters implements Criterion
{
    /** 需要搜索的属性 */
    private String[] fields;
    /** 查询文本 */
    private String query;
    /** 解析多个搜索文本（如 this is a test)时，使用and还是or操作 */
    private String operator = Constants.OR;
    /** 搜索文本的分词器，仅用于搜索引擎模式 */
    private String analyzer;
    /** 是否使用词组搜索 */
    private boolean phrase;

    public MatchExpression(String query, String... fields)
    {
        this.query = query;
        this.fields = fields;
    }

    public String[] getFields()
    {
        return fields;
    }

    public void setFields(String[] fields)
    {
        this.fields = fields;
    }

    public String getQuery()
    {
        return query;
    }

    public void setQuery(String query)
    {
        this.query = query;
    }

    public String getOperator()
    {
        return operator;
    }

    public void setOperator(String operator)
    {
        this.operator = operator;
    }

    public String getAnalyzer()
    {
        return analyzer;
    }

    public void setAnalyzer(String analyzer)
    {
        this.analyzer = analyzer;
    }

    public boolean isPhrase()
    {
        return phrase;
    }

    public void setPhrase(boolean phrase)
    {
        this.phrase = phrase;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("Match(");
        sb.append(StringUtils.join(fields, ',')).append(" ");
        sb.append(query).append(" ");
        sb.append(operator).append(" ");
        sb.append(analyzer).append(" ");
        sb.append(phrase);
        sb.append(")");
        return sb.toString();
    }
}
